'''
@Project ：my_python_project 
@File    ：迭代器.py
@IDE     ：PyCharm 
@Author  ：xionglinhui
@Date    ：2024/8/19 上午9:10 
'''


# # 1、可迭代的对象：但凡内置有__iter__方法的都称之为可迭代的对象
# s1=''
# s1.__iter__()
# print(f'【字符串】迭代器对象s1.__iter__()的值为：{s1.__iter__()}')
#
# l=[]
# l.__iter__()
# print(f'【列表】  迭代器对象l.__iter__()的值为：{l.__iter__()}')
#
# t=(1,)
# t.__iter__()
# print(f'【元组】  迭代器对象t.__iter__()的值为：{t.__iter__()}')
#
# d={'a':1}
# d.__iter__()
# print(f'【字典】  迭代器对象d.__iter__()的值为：{d.__iter__()}')
#
# set1={1,2,3}
# set1.__iter__()
# print(f'【集合】  迭代器对象set1.__iter__()的值为：{set1.__iter__()}')
#
# with open('a.txt',mode='w') as f:
#     f.__iter__()
#     print(f'【文件】  迭代器对象l.__iter__()的值为：{l.__iter__()}')
#     pass


# 2、调用可迭代对象下的__iter__方法会将其转换成迭代器对象
d={'a':1,'b':2,'c':3}
d_iterator=d.__iter__()
# print(d)
# print(d_iterator) # <dict_keyiterator object at 0x7f09e7f63318>
#
# print(d_iterator.__next__())  # d_iterator.__next__()一个一个取值，每次只能取一个
# print(d_iterator.__next__())
# print(d_iterator.__next__())
#
# print(d_iterator.__next__()) # d_iterator.__next__()值取空了，继续取导致取不到值，返回错误 StopIteration


# d={'a':1,'b':2,'c':3}
# d_iterator=d.__iter__()
#
# while True:
#     try:
#         print(d_iterator.__next__())
#     except StopIteration:
#         break

# dic={'a':1,'b':2,'c':3}
#
# dic_iterator=dic.__iter__()
# print(dic_iterator is dic_iterator.__iter__().__iter__().__iter__())

with open('a.txt',mode='w') as f:

	print(f'=============文件本身=============》：{f}')
	f.__iter__()
	print(f'【文件】  迭代器对象l.__iter__()的值为：{f.__iter__()}')
	pass


# <_io.TextIOWrapper name='a.txt' mode='w' encoding='UTF-8'>
# <_io.TextIOWrapper name='a.txt' mode='w' encoding='UTF-8'>